package com.chinacoal.microservice.impl.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.api.WorkflowConfigClient;
import com.chinacoal.microservice.exception.MessageException;
import com.chinacoal.microservice.impl.service.ProcessBusinessService;
import com.chinacoal.microservice.impl.service.ProcessService;
import com.chinacoal.microservice.impl.service.WorkFlowService;
import com.chinacoal.microservice.impl.util.ProcessFun;
import com.chinacoal.microservice.login.LoginUser;
import com.chinacoal.microservice.model.IamUser;
import com.chinacoal.microservice.model.wofkflow.QueryProcessBusinessParam;
import com.chinacoal.microservice.model.wofkflow.QueryProcessParam;
import com.chinacoal.microservice.model.wofkflow.entity.CatalogEntity;
import com.chinacoal.microservice.model.wofkflow.entity.ProcessBusinessEntity;
import com.chinacoal.microservice.model.wofkflow.entity.ProcessEntity;
import com.chinacoal.microservice.model.wofkflow.resource.WrapAttribute;
import com.chinacoal.microservice.model.wofkflow.resource.WrapProcessCatalog;
import com.chinacoal.microservice.util.result.CodeMsg;
import com.chinacoal.microservice.util.result.Result;
import com.eos.workflow.api.IBPSServiceClient;
import com.eos.workflow.data.WFActivityDefine;

/**  
 * @Package: com.chinacoal.microservice.impl.controller.WorkflowConfigController
 * @ClassName: WorkflowConfigController
 * @ClassDesc: 工作流配置Controller
 * @Createby: sunh
 * @CreatedDate: 2019-12-02 12:51:56
 */  
@RestController
public class WorkflowConfigController implements WorkflowConfigClient {

	@Resource
	private ProcessBusinessService ProcessBusinessService;
	@Resource
	private ProcessService processService;
	@Resource
	private WorkFlowService workFlowService;
	@Resource
	private LoginUser loginUser;

	@Override
	public Result<Page<ProcessBusinessEntity>> findList(Integer page, Integer pageSize, QueryProcessBusinessParam queryParam) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			queryParam.setUserId(iamUser.getUserId());
			
			Page<ProcessBusinessEntity> pages = ProcessBusinessService.findList(page, pageSize, queryParam);
			return Result.success(pages);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<ProcessBusinessEntity> findOne(String mapId) {
		try {
			ProcessBusinessEntity processBusinessEntity = ProcessBusinessService.selectById(mapId);
			return Result.success(processBusinessEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<ProcessBusinessEntity> getProcessByBusinessCode(Integer orgId, String sysCode, String businessCode) {
		try {
			ProcessBusinessEntity processBusinessEntity = ProcessBusinessService.getProcessByBusinessCode(orgId, sysCode, businessCode);
			return Result.success(processBusinessEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<Object> insertProcessBusiness(ProcessBusinessEntity processBusinessEntity) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			processBusinessEntity.setCreatedBy(iamUser.getUserId());
			processBusinessEntity.setCreatedByName(iamUser.getName());
			processBusinessEntity.setCreationDate(new Date());
			
			String mapId = ProcessBusinessService.insertProcessBusiness(processBusinessEntity);
			return Result.success(mapId);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> updateProcessBusiness(ProcessBusinessEntity processBusinessEntity) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			processBusinessEntity.setLastUpdatedBy(iamUser.getUserId());
			processBusinessEntity.setLastUpdatedByName(iamUser.getName());
			processBusinessEntity.setLastUpdateDate(new Date());
			
			String mapId = ProcessBusinessService.updateProcessBusiness(processBusinessEntity);
			return Result.success(mapId);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<Object> deleteProcessBusiness(String mapId) {
		try {
			Boolean flag = ProcessBusinessService.deleteById(mapId);
			return Result.success(flag);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}
	
	@Override
	public Result<Page<CatalogEntity>> getProcessCatalog(Integer page, Integer pageSize, QueryProcessParam queryParam) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			queryParam.setUserId(iamUser.getUserId());
			
			Page<CatalogEntity> pages = processService.getProcessCatalog(page, pageSize, queryParam);
			return Result.success(pages);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Page<ProcessEntity>> getProcessList(Integer page, Integer pageSize, QueryProcessParam queryParam) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			queryParam.setUserId(iamUser.getUserId());
			
			Page<ProcessEntity> pages = processService.getProcessList(page, pageSize, queryParam);
			return Result.success(pages);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<List<WrapAttribute>> getProcessActivity(Long processId) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			// 获取工作流服务
			IBPSServiceClient client = workFlowService.getIBPSServiceClient(iamUser.getUserId(),iamUser.getName());
			
			// 获取活动列表
			List<WFActivityDefine> list = processService.getProcessActivity(client, processId);
			
			// 格式转换
			List<WrapAttribute> activityList =  ProcessFun.getOptionListWAD(list,true,null);
			
			return Result.success(activityList);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.ERROR_WORKFLOW_ACTIVITYLIST.fillArgs(ProcessFun.getExceptionMessage(e)));
		}
	}
	
	@Override
	public Result<WrapProcessCatalog> getProcessTree(Long processId, String parentCatalogId) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			Integer userId = iamUser.getUserId();
			String name = iamUser.getName();
			
			WrapProcessCatalog wrapProcessCatalog = processService.getProcessTree(userId, name, processId, parentCatalogId);
			return Result.success(wrapProcessCatalog);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.ERROR_WORKFLOW_PROCESSTREE.fillArgs(ProcessFun.getExceptionMessage(e)));
		}
	}

	@Override
	public Result<String> getFlowChart(String instanceId) {
		try {
			String flowChart = processService.getFlowChart(instanceId);
			return Result.success(flowChart);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.ERROR_WORKFLOW_PROCESSTREE.fillArgs(ProcessFun.getExceptionMessage(e)));
		}
	}
}
